package domain

import (
	"crypto/sha1"
	"fmt"
	"gorm.io/gorm"
	"io"
	"time"
)

type Account struct {
	gorm.Model
	ID         int64     `json:"id"`
	UserID     *int64    `json:"-"`
	PlatFormID *int64    `json:"-"`
	Account    *string   `json:"account" gorm:"type:varchar(255)"`
	Password   *string   `json:"-" gorm:"type:varchar(255)"`
	Token      *string   `json:"-" gorm:"type:varchar(255)"`
	Expire     *int64    `json:"expire" gorm:"type:timestamp"`
	CreatedAt  time.Time `json:"created_at" `
	UpdatedAt  time.Time `json:"updated_at" `
	User       *User     `json:"user" gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	PlatForm   *PlatForm `json:"plat_form" gorm:"foreignKey:PlatFormID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

type AccountDTO struct {
	ID         int64        `json:"id"`
	UserID     *int64       `json:"-"`
	PlatFormID *int64       `json:"-"`
	Account    *string      `json:"account" gorm:"type:varchar(255)"`
	Password   *string      `json:"-" gorm:"type:varchar(255)"`
	Token      *string      `json:"-" gorm:"type:varchar(255)"`
	Expire     *int64       `json:"expire" gorm:"type:timestamp"`
	CreatedAt  time.Time    `json:"created_at" `
	UpdatedAt  time.Time    `json:"updated_at" `
	User       *UserDTO     `json:"user" gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	PlatForm   *PlatFormDTO `json:"plat_form" gorm:"foreignKey:PlatFormID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

func HashPassword(password string) (string, error) {
	h := sha1.New()
	_, err := io.WriteString(h, password)
	if err != nil {
		return "", err
	}
	_, err = io.WriteString(h, "club_service")
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%x", h.Sum(nil)), nil

}
